package com.sqlcheck.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * 扫描任务进度阶段枚举
 * 覆盖从任务创建到完成的所有关键节点
 */
@Getter
@RequiredArgsConstructor
public enum TaskStage {
    
    // 初始化阶段 (0-5%)
    INITIALIZATION("任务初始化", 0, 2),
    CONFIG_VALIDATION("配置验证", 2, 5),
    
    // Git任务特有阶段 (5-25%)
    GIT_CLONE_START("开始下载代码", 5, 8),
    GIT_CLONING("正在下载代码", 8, 18),
    GIT_CLONE_COMPLETE("代码下载完成", 18, 20),
    CODE_SCANNING("扫描源码文件", 20, 25),
    
    // 数据库DDL任务特有阶段 (5-25%)
    DB_CONNECTING("连接数据库", 5, 10),
    DDL_EXTRACTING("提取DDL语句", 10, 25),
    
    // SQL提取阶段 (25-35%)
    SQL_EXTRACTION_START("开始SQL提取", 25, 27),
    SQL_PARSING("解析SQL语句", 27, 32),
    SQL_EXTRACTION_COMPLETE("SQL提取完成", 32, 35),
    
    // SQL保存阶段 (35-40%)
    SQL_SAVING("保存SQL语句", 35, 40),
    
    // 规则分析阶段 (40-70%) - 细分为多个子阶段
    RULE_ANALYSIS_START("开始规则分析", 40, 42),
    RULE_LOADING("加载兼容性规则", 42, 45),
    RULE_MATCHING("匹配规则模式", 45, 60),
    RULE_RESULT_PROCESSING("处理规则结果", 60, 68),
    RULE_ANALYSIS_COMPLETE("规则分析完成", 68, 70),
    
    // AI增强验证阶段 (70-80%) - 详细细分
    AI_PREPARATION("AI验证准备", 70, 72),
    AI_DATA_SAVING("保存规则结果", 72, 74),
    AI_VERIFICATION_START("开始AI验证", 74, 75),
    AI_BATCH_PROCESSING("AI批量验证", 75, 78),
    AI_VERIFICATION_COMPLETE("AI验证完成", 78, 80),
    
    // 结果处理阶段 (80-95%)
    RESULT_AGGREGATION("聚合分析结果", 80, 85),
    RESULT_SAVING("保存最终结果", 85, 90),
    STATISTICS_UPDATE("更新统计信息", 90, 95),
    
    // 完成阶段 (95-100%)
    CLEANUP("清理临时文件", 95, 98),
    COMPLETION("任务完成", 98, 100);
    
    private final String displayName;
    private final int startProgress;
    private final int endProgress;
    
    /**
     * 计算当前阶段内的具体进度
     * 
     * @param currentStep 当前步骤
     * @param totalSteps 总步骤数
     * @return 具体进度百分比
     */
    public int calculateProgress(int currentStep, int totalSteps) {
        if (totalSteps <= 0) return startProgress;
        
        double stageRange = endProgress - startProgress;
        double stepProgress = Math.min(1.0, (double) currentStep / totalSteps);
        
        return startProgress + (int) (stageRange * stepProgress);
    }
    
    /**
     * 获取阶段中间进度
     */
    public int getMidProgress() {
        return (startProgress + endProgress) / 2;
    }
    
    /**
     * 检查进度是否在当前阶段范围内
     */
    public boolean contains(int progress) {
        return progress >= startProgress && progress <= endProgress;
    }
    
    /**
     * 根据任务类型获取适用的阶段序列
     */
    public static TaskStage[] getStagesForTaskType(TaskType taskType) {
        switch (taskType) {
            case GIT_SOURCE_SCAN:
                return new TaskStage[]{
                    INITIALIZATION, CONFIG_VALIDATION,
                    GIT_CLONE_START, GIT_CLONING, GIT_CLONE_COMPLETE, CODE_SCANNING,
                    SQL_EXTRACTION_START, SQL_PARSING, SQL_EXTRACTION_COMPLETE,
                    SQL_SAVING,
                    RULE_ANALYSIS_START, RULE_LOADING, RULE_MATCHING, RULE_RESULT_PROCESSING, RULE_ANALYSIS_COMPLETE,
                    AI_PREPARATION, AI_DATA_SAVING, AI_VERIFICATION_START, AI_BATCH_PROCESSING, AI_VERIFICATION_COMPLETE,
                    RESULT_AGGREGATION, RESULT_SAVING, STATISTICS_UPDATE,
                    CLEANUP, COMPLETION
                };
                
            case DATABASE_DDL_SCAN:
                return new TaskStage[]{
                    INITIALIZATION, CONFIG_VALIDATION,
                    DB_CONNECTING, DDL_EXTRACTING,
                    SQL_EXTRACTION_START, SQL_PARSING, SQL_EXTRACTION_COMPLETE,
                    SQL_SAVING,
                    RULE_ANALYSIS_START, RULE_LOADING, RULE_MATCHING, RULE_RESULT_PROCESSING, RULE_ANALYSIS_COMPLETE,
                    AI_PREPARATION, AI_DATA_SAVING, AI_VERIFICATION_START, AI_BATCH_PROCESSING, AI_VERIFICATION_COMPLETE,
                    RESULT_AGGREGATION, RESULT_SAVING, STATISTICS_UPDATE,
                    CLEANUP, COMPLETION
                };
                
            case MANUAL_SQL:
                return new TaskStage[]{
                    INITIALIZATION, CONFIG_VALIDATION,
                    SQL_EXTRACTION_START, SQL_PARSING, SQL_EXTRACTION_COMPLETE,
                    RULE_ANALYSIS_START, RULE_LOADING, RULE_MATCHING, RULE_RESULT_PROCESSING, RULE_ANALYSIS_COMPLETE,
                    AI_PREPARATION, AI_DATA_SAVING, AI_VERIFICATION_START, AI_BATCH_PROCESSING, AI_VERIFICATION_COMPLETE,
                    RESULT_AGGREGATION, RESULT_SAVING, STATISTICS_UPDATE,
                    COMPLETION
                };
                
            default:
                return values(); // 返回所有阶段
        }
    }
}